package com.csdj.service;

import com.csdj.mapper.UserMapper;
import com.csdj.pojo.LoginRecord;
import com.csdj.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean login(String userName, String passWord, String ip, String data) {
        System.out.println("");
        List<User> users = userMapper.selectAll(userName,passWord);
        if(users.size()!=0){
            for (User user : users) {
                long user_id=user.getId();
                LoginRecord loginRecord=new LoginRecord();
                loginRecord.setUserId(Math.toIntExact(user_id));
                loginRecord.setLoginData(Date.valueOf(data));
                loginRecord.setIp(ip);
                userMapper.insertLoginRecord(loginRecord);
                userMapper.updateLoginLastTime(user_id,data,ip);
                return true;
        }
        }
        return false;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public List<User> selectAll(String userName, String passWord) {
        return userMapper.selectAll(userName,passWord);
    }

    @Override
    public List<User> selectAllByRole(Integer role) {

        return userMapper.selectAllByRole(role);
    }

    @Override
    public Integer updatePwd(String pwd, Integer id) {
        return userMapper.updatePwd(pwd,id);
    }
}
